package cn.dida.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 日期工具类
 * @author weiwei
 *
 */
public class DateUtil {

	private static final Log loger = LogFactory.getLog(DateUtil.class);

	public static final String DATE_FORMAT = "yyyy-MM-dd";// 转换日期格式

	public static final String DATE_FORMAT_NO_ = "yyyyMMdd";// 转换日期格式

	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";// 转换日期时间格式

	public static final String TIME_FORMAT = "HH:ss:mm";// 转换时间格式

	public static Date formatStrToDate(String datestr, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);

		Date date = null;
		try {
			date = sdf.parse(datestr);

		} catch (Exception e) {
			// ExceptionUtil.logerErrorMes(loger, e);
			// System.out.println("输入日期格式错误，返回当前日期时间格式");
			return new Date();
		}
		return date;
	}

	public static Long formatStrToLong(String datestr, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);

		Date date = null;
		try {
			date = sdf.parse(datestr);

		} catch (Exception e) {
			// ExceptionUtil.logerErrorMes(loger, e);
			// System.out.println("输入日期格式错误，返回当前日期时间格式");
			return 0L;
		}
		return date.getTime();
	}

	/**
	 * 将时间转换为指定格式的字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToStr(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String datestr = "";
		try {
			datestr = sdf.format(date);
		} catch (Exception e) {
			// ExceptionUtil.logerErrorMes(loger, e);
			// System.out.println("输入日期格式错误，返回当前日期时间格式");
			return datestr;
		}
		return datestr;
	}

	public static Date strToDate(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (Exception e) {
			// ExceptionUtil.logerErrorMes(loger, e);
			// System.out.println("输入日期格式错误，返回当前日期时间格式");
			return date;
		}
		return date;
	}

	/**
	 * 从当前日期算起，获取N天前的日期（当前日不算在内），日期格式为yyyy-MM-dd
	 *
	 * @param daily
	 *            天数
	 * @return
	 */
	public static String getDateByDay(Integer daily) {
		Date date = new Date();
		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - daily;
		if (day < 1) {
			month -= 1;
			if (month == 0) {
				year -= 1;
				month = 12;
			}
			if (month == 4 || month == 6 || month == 9 || month == 11) {
				day = 30 + day;
			} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
					|| month == 12) {
				day = 31 + day;
			} else if (month == 2) {
				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
					day = 29 + day;
				} else {
					day = 28 + day;
				}
			}

		}
		String y = year + "";
		String m = "";
		String d = "";
		if (month < 10) {
			m = "0" + month;
		} else {
			m = month + "";
		}
		if (day < 10) {
			d = "0" + day;
		} else {
			d = day + "";
		}
		return y + "-" + m + "-" + d;
	}

	/**
	 * 从当前日期算起，获取N个月前的日期，日期格式为yyyy-MM-dd
	 *
	 * @param mon
	 *            月份
	 * @return
	 */
	public static String getDateByMonth(Integer mon) {
		Date date = new Date();
		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - mon;
		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
		if (month == 0) {
			year -= 1;
			month = 12;
		} else if (day > 28) {
			if (month == 2) {
				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
					day = 29;
				} else {
					day = 28;
				}
			} else if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
				day = 30;
			}
		}
		String y = year + "";
		String m = "";
		String d = "";
		if (month < 10) {
			m = "00" + month;
		} else {
			m = month + "";
		}
		if (day < 10) {
			d = "0" + day;
		} else {
			d = day + "";
		}
		return y + "-" + m + "-" + d;
	}

	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	public static long daysBetween(Date start, Date end) {
		long diff = 0;
		if (start == null || end == null) {
			return diff;
		}
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		try {
			start = f.parse(f.format(start));
			end = f.parse(f.format(end));
		} catch (ParseException e) {
			// ExceptionUtil.logerErrorMes(loger, e);
			return diff;
		}
		Calendar c = Calendar.getInstance();
		if (start.before(end)) {
			c.setTime(start);
			while (c.getTime().before(end)) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				diff++;
			}
		} else {
			c.setTime(start);
			while (c.getTime().after(end)) {
				c.add(Calendar.DAY_OF_MONTH, -1);
				diff--;
			}
		}
		return diff;
	}

	public static String getStrFromDate(Date date, String dateformat) {

		return dateToStr(date, dateformat);
		// if(null==date){
		// return "";
		// }
		// int d_minutes=-1,d_hours=-1,d_days=-1;
		// long d;
		// d = new Date().getTime() - date.getTime();
		// d = d/1000;
		// d_days = (int) (d/86400);
		// d_hours = (int) (d/3600);
		// d_minutes = (int) (d/60);
		// if(d_days>0 && d_days<7){
		// return d_days+"天前";
		// }else if(d_days<=0 && d_hours>0){
		// return d_hours+"小时前";
		// }else if(d_hours<=0 && d_minutes>0){
		// return d_minutes+"分钟前";
		// }else{
		// return dateToStr(date, DATE_FORMAT);
		// }
	}

	/**
	 * 比较两个时间大小
	 * 
	 * @param date1
	 * @param date2
	 * @return date1大于date2 返回1，date1等于date2 返回0, date1少于date2 返回-1
	 */
	public static int compare(Date date1, Date date2) {
		return date1.compareTo(date2);
	}

	public static void main(String[] args) {
		// String str = getDateByDay(2);
		// System.out.println(str);
		Date d1 = new Date(2012 - 1900, 9 - 1, 20);
		Date d2 = new Date(2010 - 1900, 7 - 1, 31);

		// Date d = getDateAfter(d1, 11);
		// System.out.println(d.toLocaleString());

		Long l = daysBetween(d2, d1);
		// Date d3 = new Date(l);
		System.out.println(l);
	}

	public static Long getLongTime(String dateString)throws Exception{
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
		Date date = df.parse(dateString);
		Long s=date.getTime();
		return s;
	}

}
